<?php

//function connect database
function DbConnect()
{
   //$mysqli = new mysqli("localhost", "pcdungco", "chinhdung", "pcdungco_blog");
   $mysqli = new mysqli("localhost", "root", "", "oseeyou");
   if(!$mysqli)
   {
      exit();
   }
   
   //set charset in mysql
   $mysqli->set_charset("utf8");
   
   //turn on commiting in database
   $mysqli->autocommit(true);
   
   return $mysqli;
}	

//function save values in array when query select action.
function DbResultArray($result)
{
   $result_array = array();
   
   for($i = 0; $row = $result->fetch_assoc(); $i++)
      $result_array[$i] = $row;

   return $result_array;
}

?>

<?php
//function insert, select, update, delete data in database
function DbInsert($query)
{
   $connect = DbConnect();
   
   $result = @$connect->query($query);
   if(!$result)
   {
      
      return false;
   }
   
   $connect->close();
   return true;
}

//Query action select in database
function DbSelect($query)
{
   $connect = DbConnect();
   
   $result = @$connect->query($query);
   if(!$result)
   {
      return false;
   }
   
   $rows = $result->num_rows;
   
   if($rows == 0)
      return false;
   
   //result saved in array. call to function DbResultArray()
   $result = DbResultArray($result);
   
   $connect->close();

   return $result;
}

//Query delete in database
function DbDelete($query)
{
   $connect = DbConnect();
   
   $result = @$connect->query($query);
   if(!$result)
   {
      
      return false;
   }
   
   $connect->close();

   return true;
}

//Query action updata in database
function DbUpdate($query)
{
   $connect = DbConnect();
   
   $result = @$connect->query($query);
   if(!$result)
   {
      
      return false;
   }
   
   $connect->close();

   return true;
}
?>

<?php

//get rows in database
function DbGetRows($query)
{
   $connect = DbConnect();
   
   $result = @$connect->query($query);
   
   if(!$query)
      return false;
   
   $rows = $result->num_rows;
   
   $connect->close();
   return $rows;
}

//function login
function DbLogin($email, $password)
{
   $connect = DbConnect();
   
   $query = "SELECT * FROM `oUsers` WHERE `email` = '$email' and `password` = '$password'";
   
   $result = @$connect->query($query);
   
   if(!$result)
      return false;
   $rows = $result->num_rows;
      
   $connect->close();
   
   if($rows == 0)
      return false;
   
   $result = DbResultArray($result);
   return $result;
}

?>
